


ALTER         FUNCTION fn_ct_mkg_load_marketing_forecast_details(

@CUSTOMER VARCHAR(100),
@PRODUCT VARCHAR(100),
@MONTH_FROM INT,
@MONTH_TO INT,
@YEAR INT )

--MISC ADDED BY VARMA TO DIFFERENTIATE ACTUAL FORECAST AND MISC FORECAST
RETURNS @FN_RESULTS TABLE( CUSTOMER VARCHAR(100), PRODUCT VARCHAR(100),
MONTH INT, YEAR INT, SF_QUANTITY INT, SF_UNIT_PRICE FLOAT, SF_CONFIDENCE_LEVEL FLOAT, MF_QUANTITY INT, MF_UNIT_PRICE FLOAT, MF_CONFIDENCE_LEVEL FLOAT ,MISC VARCHAR(15) )

AS
BEGIN


DECLARE @MKTG_RESULTS TABLE( MF_ID INT, USR VARCHAR(50), CUSTOMER VARCHAR(100), PRODUCT VARCHAR(100),
MONTH INT, MONTH_NAME VARCHAR(20), YEAR INT, QUANTITY INT, UNIT_PRICE FLOAT, CREATED_DATE VARCHAR(20), MODIFIED_DATE VARCHAR(20), CONFIDENCE_LEVEL FLOAT )

DECLARE @SALES_RESULTS TABLE( SF_ID INT, USR VARCHAR(50), CUSTOMER VARCHAR(100), PRODUCT VARCHAR(100),
MONTH INT, MONTH_NAME VARCHAR(20), YEAR INT, QUANTITY INT, UNIT_PRICE FLOAT, CREATED_DATE VARCHAR(20), MODIFIED_DATE VARCHAR(20), CONFIDENCE_LEVEL FLOAT )

DECLARE @SF_RESULTS TABLE( CUSTOMER VARCHAR(100), PRODUCT VARCHAR(100), MONTH INT, YEAR INT, QUANTITY INT, UNIT_PRICE FLOAT, CONFIDENCE_LEVEL FLOAT )

--Added by Gandhi 0n 3rd Oct 2004
DECLARE @MISC_RESULTS TABLE( CUSTOMER VARCHAR(100), PRODUCT VARCHAR(100), MONTH INT, YEAR INT, QUANTITY INT, UNIT_PRICE FLOAT, CONFIDENCE_LEVEL FLOAT )
-- Ends here - Gandhi


	IF RTRIM(@MONTH_FROM) = -1
	BEGIN
		SET @MONTH_FROM = 0
	END

	IF RTRIM(@MONTH_TO) = -1
	BEGIN
		SET @MONTH_TO = 11
	END
	
	DECLARE @TO_MONTH INT	-- Added by Gandhi on 21st Aug 2004
	SET @TO_MONTH = @MONTH_FROM + @MONTH_TO -- Added by Gandhi on 21st Aug 2004

	DECLARE @YEAR_TO INT
	/*SET @YEAR_TO = @YEAR + @MONTH_TO/12    -- Commented by Gandhi on 21st Aug 2004
	SET @MONTH_TO = @MONTH_TO%12*/
	
	SET @YEAR_TO = @YEAR + @TO_MONTH/12 -- Added by Gandhi on 21st Aug 2004
	SET @MONTH_TO = @TO_MONTH%12	-- Added by Gandhi on 21st Aug 2004	

	-- Added by Gandhi on 12th Aug 2004
	DECLARE @ROW_COUNT INT	
	DECLARE @TEMP INT
	
	DECLARE @SF_ID INT
	DECLARE @SALES_PERSON VARCHAR(50)
	DECLARE @SF_CUSTOMER VARCHAR(100)
	DECLARE @SF_PRODUCT VARCHAR(50)
	DECLARE @SF_MONTH INT
	DECLARE @SF_MONTH_NAME VARCHAR(20)
	DECLARE @SF_YEAR INT
	DECLARE @SF_QUANTITY INT
	DECLARE @SF_OLD_QUANTITY INT
	DECLARE @SF_UNIT_PRICE FLOAT
	DECLARE @SF_OLD_UNIT_PRICE FLOAT
	DECLARE @SF_PRICE FLOAT	
	DECLARE @CREATED_DATE datetime
	DECLARE @MODIFIED_DATE datetime

	DECLARE @SF_CONFIDENCE_LEVEL FLOAT  -- Added by gandhi on 20th Aug 2004
	DECLARE @MF_CONFIDENCE_LEVEL FLOAT  -- Added by gandhi on 20th Aug 2004

	DECLARE @MKTG_PART_NUMBER VARCHAR(100) -- Added by gandhi on 20th Aug 2004

	DECLARE @AVG_SF_CONFIDENCE_LEVEL FLOAT
		
	DECLARE @TEMP_CURSOR CURSOR 
	
	DECLARE @SF_CURSOR CURSOR  -- Added by Gandhi on 2nd Sep 2004

	DECLARE @MISC_CURSOR CURSOR  -- Added by Gandhi on 3rd Oct 2004	
	
	-- Ends here - Gandhi
	
	IF @CUSTOMER <> '0' AND @PRODUCT <> '0'
	BEGIN
		SET @MF_CONFIDENCE_LEVEL = 100.00
		
		INSERT @MKTG_RESULTS (MF_ID, USR , CUSTOMER , PRODUCT , MONTH , MONTH_NAME , YEAR , QUANTITY , UNIT_PRICE , CREATED_DATE , MODIFIED_DATE , CONFIDENCE_LEVEL)
		SELECT MF_ID[ID], CREATED_BY[USER], CUSTOMER, P_Marketing_part_Number[PRODUCT] , MF_MONTH[MONTH],
		MONTH_NAME = CASE MF_MONTH
			         WHEN 0  THEN 'Jan ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 1  THEN 'Feb ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 2  THEN 'Mar ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 3  THEN 'Apr ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 4  THEN 'May ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 5  THEN 'Jun ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 6  THEN 'Jul ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 7  THEN 'Aug ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 8  THEN 'Sep ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 9  THEN 'Oct ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 10 THEN 'Nov ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 11 THEN 'Dec ' + CONVERT(VARCHAR, MF_YEAR)
			      END,
		MF_YEAR[YEAR], MF_QTY[QUANTITY], MF_UNIT_PRICE[UNIT_PRICE], CONVERT(VARCHAR, CREATED_DT, 101)[CREATED_DATE], CONVERT(VARCHAR, CREATED_DT, 101)[MODIFIED_DATE], @MF_CONFIDENCE_LEVEL
 		FROM MARKETING_FORECAST_MASTER MFM 
		INNER JOIN Product P ON P.Part_Number = MFM.PRODUCT
		INNER JOIN Process_Step PS ON PS.ProcessStep_ID = P.ProcessStep_ID
		WHERE MFM.CUSTOMER = @CUSTOMER AND MFM.PRODUCT = @PRODUCT AND PS.PS_Type_Name LIKE 'Finished Goods' 
		AND ( ( MF_MONTH >= @MONTH_FROM AND MF_YEAR = @YEAR AND MF_MONTH < @TO_MONTH )	OR
		      ( MF_MONTH < @MONTH_TO AND MF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO) OR
		      ( MF_YEAR < @YEAR_TO AND MF_YEAR > @YEAR ) ) -- Query modified by Gandhi on 23rd Aug 2004
		
		-- Added by Gandhi on 12th Aug 2004 to get sales forecast
		SET @TEMP_CURSOR = CURSOR FOR
		SELECT SF_ID[ID], SALES_PERSON[USER], CUSTOMER, PRODUCT, SF_MONTH[MONTH],
		MONTH_NAME = CASE SF_MONTH
			         WHEN 0  THEN 'Jan ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 1  THEN 'Feb ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 2  THEN 'Mar ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 3  THEN 'Apr ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 4  THEN 'May ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 5  THEN 'Jun ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 6  THEN 'Jul ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 7  THEN 'Aug ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 8  THEN 'Sep ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 9  THEN 'Oct ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 10 THEN 'Nov ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 11 THEN 'Dec ' + CONVERT(VARCHAR, SF_YEAR)
			      END,
		SF_YEAR[YEAR], SF_QTY[QUANTITY], SF_UNIT_PRICE[UNIT_PRICE], CONVERT(VARCHAR, CREATED_DT, 101)[CREATED_DATE], CONVERT(VARCHAR, CREATED_DT, 101)[MODIFIED_DATE], SF_CONFIDENCE_LEVEL
		FROM SALES_FORECAST_MASTER 
		WHERE CUSTOMER = @CUSTOMER 
		AND PRODUCT = @PRODUCT 
		AND ( ( SF_MONTH >= @MONTH_FROM AND SF_YEAR = @YEAR AND SF_MONTH < @TO_MONTH ) OR
		      ( SF_MONTH < @MONTH_TO AND SF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO ) OR
		      ( SF_YEAR < @YEAR_TO AND SF_YEAR > @YEAR ) ) -- Query modified by Gandhi on 23rd Aug 2004

		-- Ends here - Gandhi
		-- Added by Gandhi on 2nd Sep 2004 to get sales forecast
		SET @SF_CURSOR = CURSOR FOR
		SELECT CUSTOMER, PRODUCT, SF_MONTH[MONTH],SF_YEAR[YEAR], SF_QTY[QUANTITY], SF_UNIT_PRICE[UNIT_PRICE],SF_CONFIDENCE_LEVEL
		FROM SALES_FORECAST_MASTER 
		WHERE CUSTOMER = @CUSTOMER 
		AND PRODUCT = @PRODUCT 
		AND ( ( SF_MONTH >= @MONTH_FROM AND SF_YEAR = @YEAR AND SF_MONTH < @TO_MONTH ) OR
		      ( SF_MONTH < @MONTH_TO AND SF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO ) OR
		      ( SF_YEAR < @YEAR_TO AND SF_YEAR > @YEAR ) ) 
		-- Ends here - Gandhi
        END


	ELSE IF @CUSTOMER <> '0' AND @PRODUCT = '0'
	BEGIN	
		SET @MF_CONFIDENCE_LEVEL = 100.00

		INSERT @MKTG_RESULTS (MF_ID, USR , CUSTOMER , PRODUCT , MONTH , MONTH_NAME , YEAR , QUANTITY , UNIT_PRICE , CREATED_DATE , MODIFIED_DATE , CONFIDENCE_LEVEL)
		SELECT MF_ID[ID], CREATED_BY[USER], CUSTOMER, P_Marketing_part_Number[PRODUCT] , MF_MONTH[MONTH],
		MONTH_NAME = CASE MF_MONTH
			         WHEN 0  THEN 'Jan ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 1  THEN 'Feb ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 2  THEN 'Mar ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 3  THEN 'Apr ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 4  THEN 'May ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 5  THEN 'Jun ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 6  THEN 'Jul ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 7  THEN 'Aug ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 8  THEN 'Sep ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 9  THEN 'Oct ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 10 THEN 'Nov ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 11 THEN 'Dec ' + CONVERT(VARCHAR, MF_YEAR)
			      END,
		MF_YEAR[YEAR], MF_QTY[QUANTITY], MF_UNIT_PRICE[UNIT_PRICE], CONVERT(VARCHAR, CREATED_DT, 101)[CREATED_DATE], CONVERT(VARCHAR, CREATED_DT, 101)[MODIFIED_DATE], @MF_CONFIDENCE_LEVEL
 		FROM MARKETING_FORECAST_MASTER MFM 
		INNER JOIN Product P ON P.Part_Number = MFM.PRODUCT
		INNER JOIN Process_Step PS ON PS.ProcessStep_ID = P.ProcessStep_ID
		WHERE MFM.CUSTOMER = @CUSTOMER AND PS.PS_Type_Name LIKE 'Finished Goods' 
		AND ( ( MF_MONTH >= @MONTH_FROM AND MF_YEAR = @YEAR AND MF_MONTH < @TO_MONTH )	OR
		      ( MF_MONTH < @MONTH_TO AND MF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO) OR
		      ( MF_YEAR < @YEAR_TO AND MF_YEAR > @YEAR ) ) -- Query modified by Gandhi on 23rd Aug 2004
		
		-- Added by Gandhi on 12th Aug 2004 to get sales forecast
		SET @TEMP_CURSOR = CURSOR FOR		SELECT SF_ID[ID], SALES_PERSON[USER], CUSTOMER, PRODUCT, SF_MONTH[MONTH],
		MONTH_NAME = CASE SF_MONTH
			         WHEN 0  THEN 'Jan ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 1  THEN 'Feb ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 2  THEN 'Mar ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 3  THEN 'Apr ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 4  THEN 'May ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 5  THEN 'Jun ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 6  THEN 'Jul ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 7  THEN 'Aug ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 8  THEN 'Sep ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 9  THEN 'Oct ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 10 THEN 'Nov ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 11 THEN 'Dec ' + CONVERT(VARCHAR, SF_YEAR)
			      END,
		SF_YEAR[YEAR], SF_QTY[QUANTITY], SF_UNIT_PRICE[UNIT_PRICE], CONVERT(VARCHAR, CREATED_DT, 101)[CREATED_DATE], CONVERT(VARCHAR, CREATED_DT, 101)[MODIFIED_DATE], SF_CONFIDENCE_LEVEL
		FROM SALES_FORECAST_MASTER 
		WHERE CUSTOMER = @CUSTOMER 
		AND ( ( SF_MONTH >= @MONTH_FROM AND SF_YEAR = @YEAR AND SF_MONTH < @TO_MONTH ) OR
		      ( SF_MONTH < @MONTH_TO AND SF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO ) OR
		      ( SF_YEAR < @YEAR_TO AND SF_YEAR > @YEAR ) ) -- Query modified by Gandhi on 23rd Aug 2004

		-- Ends here - Gandhi		
		-- Added by Gandhi on 2nd Sep 2004 to get sales forecast
		SET @SF_CURSOR = CURSOR FOR
		SELECT CUSTOMER, PRODUCT, SF_MONTH[MONTH],SF_YEAR[YEAR], SF_QTY[QUANTITY], SF_UNIT_PRICE[UNIT_PRICE],SF_CONFIDENCE_LEVEL
		FROM SALES_FORECAST_MASTER 
		WHERE CUSTOMER = @CUSTOMER 
		AND ( ( SF_MONTH >= @MONTH_FROM AND SF_YEAR = @YEAR AND SF_MONTH < @TO_MONTH ) OR
		      ( SF_MONTH < @MONTH_TO AND SF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO ) OR
		      ( SF_YEAR < @YEAR_TO AND SF_YEAR > @YEAR ) )
		-- Ends here - Gandhi
 	END

	
	ELSE IF @CUSTOMER = '0' AND @PRODUCT <> '0'
	BEGIN
		SET @MF_CONFIDENCE_LEVEL = 100.00

		INSERT @MKTG_RESULTS (MF_ID, USR , CUSTOMER , PRODUCT , MONTH , MONTH_NAME , YEAR , QUANTITY , UNIT_PRICE , CREATED_DATE , MODIFIED_DATE , CONFIDENCE_LEVEL)
		SELECT MF_ID[ID], CREATED_BY[USER], CUSTOMER, P_Marketing_part_Number[PRODUCT] , MF_MONTH[MONTH],
		MONTH_NAME = CASE MF_MONTH
			         WHEN 0  THEN 'Jan ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 1  THEN 'Feb ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 2  THEN 'Mar ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 3  THEN 'Apr ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 4  THEN 'May ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 5  THEN 'Jun ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 6  THEN 'Jul ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 7  THEN 'Aug ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 8  THEN 'Sep ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 9  THEN 'Oct ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 10 THEN 'Nov ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 11 THEN 'Dec ' + CONVERT(VARCHAR, MF_YEAR)
			      END,
		MF_YEAR[YEAR], MF_QTY[QUANTITY], MF_UNIT_PRICE[UNIT_PRICE], CONVERT(VARCHAR, CREATED_DT, 101)[CREATED_DATE], CONVERT(VARCHAR, CREATED_DT, 101)[MODIFIED_DATE], @MF_CONFIDENCE_LEVEL
 		FROM MARKETING_FORECAST_MASTER MFM 
		INNER JOIN Product P ON P.Part_Number = MFM.PRODUCT
		INNER JOIN Process_Step PS ON PS.ProcessStep_ID = P.ProcessStep_ID
		WHERE MFM.PRODUCT = @PRODUCT AND PS.PS_Type_Name LIKE 'Finished Goods' 
		AND ( ( MF_MONTH >= @MONTH_FROM AND MF_YEAR = @YEAR AND MF_MONTH < @TO_MONTH )	OR
		      ( MF_MONTH < @MONTH_TO AND MF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO) OR
		      ( MF_YEAR < @YEAR_TO AND MF_YEAR > @YEAR ) ) -- Query modified by Gandhi on 23rd Aug 2004
		
		-- Added by Gandhi on 12th Aug 2004 to get sales forecast
		SET @TEMP_CURSOR = CURSOR FOR
		SELECT SF_ID[ID], SALES_PERSON[USER], CUSTOMER, PRODUCT, SF_MONTH[MONTH],
		MONTH_NAME = CASE SF_MONTH
			         WHEN 0  THEN 'Jan ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 1  THEN 'Feb ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 2  THEN 'Mar ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 3  THEN 'Apr ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 4  THEN 'May ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 5  THEN 'Jun ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 6  THEN 'Jul ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 7  THEN 'Aug ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 8  THEN 'Sep ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 9  THEN 'Oct ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 10 THEN 'Nov ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 11 THEN 'Dec ' + CONVERT(VARCHAR, SF_YEAR)
			      END,
		SF_YEAR[YEAR], SF_QTY[QUANTITY], SF_UNIT_PRICE[UNIT_PRICE], CONVERT(VARCHAR, CREATED_DT, 101)[CREATED_DATE], CONVERT(VARCHAR, CREATED_DT, 101)[MODIFIED_DATE], SF_CONFIDENCE_LEVEL
		FROM SALES_FORECAST_MASTER 
		WHERE PRODUCT = @PRODUCT 
		AND ( ( SF_MONTH >= @MONTH_FROM AND SF_YEAR = @YEAR AND SF_MONTH < @TO_MONTH ) OR
		      ( SF_MONTH < @MONTH_TO AND SF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO ) OR
		      ( SF_YEAR < @YEAR_TO AND SF_YEAR > @YEAR ) ) -- Query modified by Gandhi on 23rd Aug 2004

		-- Ends here - Gandhi 
		-- Added by Gandhi on 2nd Sep 2004 to get sales forecast
		SET @SF_CURSOR = CURSOR FOR
		SELECT CUSTOMER, PRODUCT, SF_MONTH[MONTH],SF_YEAR[YEAR], SF_QTY[QUANTITY], SF_UNIT_PRICE[UNIT_PRICE],SF_CONFIDENCE_LEVEL
		FROM SALES_FORECAST_MASTER 
		WHERE PRODUCT = @PRODUCT 
		AND ( ( SF_MONTH >= @MONTH_FROM AND SF_YEAR = @YEAR AND SF_MONTH < @TO_MONTH ) OR
		      ( SF_MONTH < @MONTH_TO AND SF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO ) OR
		      ( SF_YEAR < @YEAR_TO AND SF_YEAR > @YEAR ) )
		
		-- Ends here - Gandhi
	END
	
	
	ELSE IF @CUSTOMER = '0' AND @PRODUCT = '0'
	BEGIN
		SET @MF_CONFIDENCE_LEVEL = 100.00
		
		INSERT @MKTG_RESULTS (MF_ID, USR , CUSTOMER , PRODUCT , MONTH , MONTH_NAME , YEAR , QUANTITY , UNIT_PRICE , CREATED_DATE , MODIFIED_DATE , CONFIDENCE_LEVEL)
		SELECT MF_ID[ID], CREATED_BY[USER], CUSTOMER, P_Marketing_part_Number[PRODUCT] , MF_MONTH[MONTH],
		MONTH_NAME = CASE MF_MONTH
			         WHEN 0  THEN 'Jan ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 1  THEN 'Feb ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 2  THEN 'Mar ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 3  THEN 'Apr ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 4  THEN 'May ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 5  THEN 'Jun ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 6  THEN 'Jul ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 7  THEN 'Aug ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 8  THEN 'Sep ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 9  THEN 'Oct ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 10 THEN 'Nov ' + CONVERT(VARCHAR, MF_YEAR)
			         WHEN 11 THEN 'Dec ' + CONVERT(VARCHAR, MF_YEAR)
			      END,
		MF_YEAR[YEAR], MF_QTY[QUANTITY], MF_UNIT_PRICE[UNIT_PRICE], CONVERT(VARCHAR, CREATED_DT, 101)[CREATED_DATE], CONVERT(VARCHAR, CREATED_DT, 101)[MODIFIED_DATE], @MF_CONFIDENCE_LEVEL
 		FROM MARKETING_FORECAST_MASTER MFM 
		INNER JOIN Product P ON P.Part_Number = MFM.PRODUCT
		INNER JOIN Process_Step PS ON PS.ProcessStep_ID = P.ProcessStep_ID
		WHERE PS.PS_Type_Name LIKE 'Finished Goods' 
		AND 
		    ( ( MF_MONTH >= @MONTH_FROM AND MF_YEAR = @YEAR AND MF_MONTH < @TO_MONTH )	OR
		      ( MF_MONTH < @MONTH_TO AND MF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO) OR
		      ( MF_YEAR < @YEAR_TO AND MF_YEAR > @YEAR ) ) -- Query modified by Gandhi on 23rd Aug 2004
					
		-- Added by Gandhi on 12th Aug 2004 to get sales forecast
		SET @TEMP_CURSOR = CURSOR FOR
		SELECT DISTINCT SF_ID[ID], SALES_PERSON[USER], CUSTOMER, PRODUCT, SF_MONTH[MONTH],
		MONTH_NAME = CASE SF_MONTH
			         WHEN 0  THEN 'Jan ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 1  THEN 'Feb ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 2  THEN 'Mar ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 3  THEN 'Apr ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 4  THEN 'May ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 5  THEN 'Jun ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 6  THEN 'Jul ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 7  THEN 'Aug ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 8  THEN 'Sep ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 9  THEN 'Oct ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 10 THEN 'Nov ' + CONVERT(VARCHAR, SF_YEAR)
			         WHEN 11 THEN 'Dec ' + CONVERT(VARCHAR, SF_YEAR)
			      END,
		SF_YEAR[YEAR], SF_QTY[QUANTITY], SF_UNIT_PRICE[UNIT_PRICE], CONVERT(VARCHAR, CREATED_DT, 101)[CREATED_DATE], CONVERT(VARCHAR, CREATED_DT, 101)[MODIFIED_DATE], SF_CONFIDENCE_LEVEL
		FROM SALES_FORECAST_MASTER 
		WHERE ( ( SF_MONTH >= @MONTH_FROM AND SF_YEAR = @YEAR AND SF_MONTH < @TO_MONTH ) OR
		        ( SF_MONTH < @MONTH_TO AND SF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO ) OR
		        ( SF_YEAR < @YEAR_TO AND SF_YEAR > @YEAR ) ) -- Query modified by Gandhi on 23rd Aug 2004
		-- Ends here - Gandhi 
		-- Added by Gandhi on 2nd Sep 2004 to get sales forecast		
		SET @SF_CURSOR = CURSOR FOR
		SELECT CUSTOMER, PRODUCT, SF_MONTH[MONTH],SF_YEAR[YEAR], SF_QTY[QUANTITY], SF_UNIT_PRICE[UNIT_PRICE],SF_CONFIDENCE_LEVEL
		FROM SALES_FORECAST_MASTER 
		WHERE ( ( SF_MONTH >= @MONTH_FROM AND SF_YEAR = @YEAR AND SF_MONTH < @TO_MONTH ) OR
		      ( SF_MONTH < @MONTH_TO AND SF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO ) OR
		      ( SF_YEAR < @YEAR_TO AND SF_YEAR > @YEAR ) )

		-- Ends here - Gandhi
	
	END	
		-- Added by Gandhi on 12th Aug 2004
		OPEN @TEMP_CURSOR 
			FETCH NEXT FROM @TEMP_CURSOR
			INTO @SF_ID, @SALES_PERSON, @SF_CUSTOMER, @SF_PRODUCT, @SF_MONTH, @SF_MONTH_NAME, @SF_YEAR, @SF_QUANTITY, @SF_UNIT_PRICE, @CREATED_DATE, @MODIFIED_DATE, @SF_CONFIDENCE_LEVEL				
			WHILE @@FETCH_STATUS = 0
			BEGIN
					SELECT @ROW_COUNT = COUNT(*) FROM @SALES_RESULTS
					WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR
					
					IF ( @SF_CONFIDENCE_LEVEL IS NULL )
					BEGIN
						SET @SF_CONFIDENCE_LEVEL = 100.00	
					END

	
						IF ( @ROW_COUNT = 0 )
					     	BEGIN
							INSERT INTO @SALES_RESULTS VALUES(@SF_ID, @SALES_PERSON, @SF_CUSTOMER, @SF_PRODUCT, @SF_MONTH, @SF_MONTH_NAME, @SF_YEAR, @SF_QUANTITY, @SF_UNIT_PRICE, CONVERT(VARCHAR, @CREATED_DATE, 101), CONVERT(VARCHAR, @MODIFIED_DATE, 101), @SF_CONFIDENCE_LEVEL)
						END
						ELSE
						BEGIN
							SELECT @SF_OLD_QUANTITY = QUANTITY,@SF_OLD_UNIT_PRICE = UNIT_PRICE FROM @SALES_RESULTS
							WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR 
							
							IF(@SF_UNIT_PRICE > @SF_OLD_UNIT_PRICE)
							BEGIN
								SET @SF_PRICE = @SF_UNIT_PRICE
							END
							ELSE
							BEGIN
								SET @SF_PRICE = @SF_OLD_UNIT_PRICE
							END

							SELECT @AVG_SF_CONFIDENCE_LEVEL = ROUND( AVG(SF_CONFIDENCE_LEVEL), 0 ) FROM SALES_FORECAST_MASTER WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND SF_MONTH = @SF_MONTH AND SF_YEAR = @SF_YEAR				
	
							UPDATE @SALES_RESULTS SET QUANTITY = (@SF_QUANTITY + @SF_OLD_QUANTITY), UNIT_PRICE = @SF_PRICE, CONFIDENCE_LEVEL = @AVG_SF_CONFIDENCE_LEVEL
							WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR
						END
	
										
					SELECT @MKTG_PART_NUMBER = Product.P_Marketing_part_Number from product,Process_Step  where Product.Part_Number = @SF_PRODUCT and product.ProcessStep_ID in (select processstep_id from Process_Step where PS_Type_Name like 'Finished Goods') and product.processStep_id=Process_Step.ProcessStep_ID  
					UPDATE @SALES_RESULTS SET PRODUCT =  @MKTG_PART_NUMBER WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR

					
					FETCH NEXT FROM @TEMP_CURSOR
					INTO @SF_ID, @SALES_PERSON, @SF_CUSTOMER, @SF_PRODUCT, @SF_MONTH, @SF_MONTH_NAME, @SF_YEAR, @SF_QUANTITY, @SF_UNIT_PRICE, @CREATED_DATE, @MODIFIED_DATE, @SF_CONFIDENCE_LEVEL			
			END	
										
					-- Added by Gandhi on 2nd Sep 2004
					SELECT @ROW_COUNT = COUNT(*) FROM @SALES_RESULTS
					SELECT @TEMP = COUNT(*) FROM @MKTG_RESULTS	
							
					IF( CONVERT(VARCHAR,@CUSTOMER) <> '0' AND CONVERT(VARCHAR,@PRODUCT) <> '0')
					BEGIN
						IF ( @ROW_COUNT <= @TEMP )
						BEGIN
							INSERT @FN_RESULTS SELECT MKTG.CUSTOMER,MKTG.PRODUCT,MKTG.MONTH,MKTG.YEAR,SALES.QUANTITY,SALES.UNIT_PRICE,SALES.CONFIDENCE_LEVEL,
							MKTG.QUANTITY,MKTG.UNIT_PRICE,MKTG.CONFIDENCE_LEVEL,'' FROM @SALES_RESULTS SALES FULL OUTER JOIN @MKTG_RESULTS MKTG ON 
							SALES.MONTH = MKTG.MONTH AND SALES.YEAR = MKTG.YEAR AND SALES.PRODUCT = MKTG.PRODUCT AND SALES.CUSTOMER = MKTG.CUSTOMER
						END
						ELSE IF ( @ROW_COUNT > @TEMP )
	
						BEGIN
							INSERT @FN_RESULTS SELECT SALES.CUSTOMER,SALES.PRODUCT,SALES.MONTH,SALES.YEAR,SALES.QUANTITY,SALES.UNIT_PRICE,SALES.CONFIDENCE_LEVEL,
							MKTG.QUANTITY,MKTG.UNIT_PRICE,MKTG.CONFIDENCE_LEVEL ,'' FROM @SALES_RESULTS SALES FULL OUTER JOIN @MKTG_RESULTS MKTG ON 
							SALES.MONTH = MKTG.MONTH AND SALES.YEAR = MKTG.YEAR AND SALES.PRODUCT = MKTG.PRODUCT AND SALES.CUSTOMER = MKTG.CUSTOMER
						END

					END										
	

					IF( ( CONVERT(VARCHAR,@CUSTOMER) = '0' AND CONVERT(VARCHAR,@PRODUCT) = '0' ) OR ( CONVERT(VARCHAR,@CUSTOMER) = '0' AND CONVERT(VARCHAR,@PRODUCT) <> '0' ) OR ( CONVERT(VARCHAR,@CUSTOMER) <> '0' AND CONVERT(VARCHAR,@PRODUCT) = '0' ))
					BEGIN
						
							INSERT @FN_RESULTS SELECT MKTG.CUSTOMER,MKTG.PRODUCT,MKTG.MONTH,MKTG.YEAR,SALES.QUANTITY,SALES.UNIT_PRICE,SALES.CONFIDENCE_LEVEL,
							MKTG.QUANTITY,MKTG.UNIT_PRICE,MKTG.CONFIDENCE_LEVEL,'' FROM @SALES_RESULTS SALES RIGHT OUTER JOIN @MKTG_RESULTS MKTG ON 
							SALES.MONTH = MKTG.MONTH AND SALES.YEAR = MKTG.YEAR AND SALES.PRODUCT = MKTG.PRODUCT AND SALES.CUSTOMER = MKTG.CUSTOMER  
					
					END										
					-- Ends here - Gandhi						
		CLOSE @TEMP_CURSOR
		DEALLOCATE @TEMP_CURSOR

		-- Ends here - Gandhi	     		

	-- Added by Gandhi on 2nd Sep 2004
	IF( ( CONVERT(VARCHAR,@CUSTOMER) = '0' AND CONVERT(VARCHAR,@PRODUCT) = '0' ) OR ( CONVERT(VARCHAR,@CUSTOMER) = '0' AND CONVERT(VARCHAR,@PRODUCT) <> '0' ) OR ( CONVERT(VARCHAR,@CUSTOMER) <> '0' AND CONVERT(VARCHAR,@PRODUCT) = '0' ))
	BEGIN

		OPEN @SF_CURSOR 
			FETCH NEXT FROM @SF_CURSOR
			INTO  @SF_CUSTOMER, @SF_PRODUCT, @SF_MONTH, @SF_YEAR, @SF_QUANTITY, @SF_UNIT_PRICE, @SF_CONFIDENCE_LEVEL				
			WHILE @@FETCH_STATUS = 0
			BEGIN
					SELECT @ROW_COUNT = COUNT(*) FROM @SF_RESULTS
					WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR
					
					SELECT @TEMP = COUNT(*) FROM MARKETING_FORECAST_MASTER
					WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MF_MONTH = @SF_MONTH AND MF_YEAR = @SF_YEAR
				
					IF ( @SF_CONFIDENCE_LEVEL IS NULL )
					BEGIN
						SET @SF_CONFIDENCE_LEVEL = 100.00	
					END

					IF( @TEMP = 0 )
					BEGIN
						
						IF ( @ROW_COUNT = 0 )
					        BEGIN
							INSERT INTO @SF_RESULTS VALUES( @SF_CUSTOMER, @SF_PRODUCT, @SF_MONTH, @SF_YEAR, @SF_QUANTITY, @SF_UNIT_PRICE, @SF_CONFIDENCE_LEVEL)
						END
						ELSE
						BEGIN
							SELECT @SF_OLD_QUANTITY = QUANTITY,@SF_OLD_UNIT_PRICE = UNIT_PRICE FROM @SF_RESULTS
							WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR 
							
							IF(@SF_UNIT_PRICE > @SF_OLD_UNIT_PRICE)
							BEGIN
								SET @SF_PRICE = @SF_UNIT_PRICE
							END
							ELSE
							BEGIN
								SET @SF_PRICE = @SF_OLD_UNIT_PRICE
							END

							SELECT @AVG_SF_CONFIDENCE_LEVEL = ROUND( AVG(SF_CONFIDENCE_LEVEL), 0 ) FROM SALES_FORECAST_MASTER WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND SF_MONTH = @SF_MONTH AND SF_YEAR = @SF_YEAR				
	
							UPDATE @SF_RESULTS SET QUANTITY = (@SF_QUANTITY + @SF_OLD_QUANTITY), UNIT_PRICE = @SF_PRICE, CONFIDENCE_LEVEL = @AVG_SF_CONFIDENCE_LEVEL
							WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR
						END
	
					END				

					
					SELECT @MKTG_PART_NUMBER = Product.P_Marketing_part_Number from product,Process_Step  where Product.Part_Number = @SF_PRODUCT and product.ProcessStep_ID in (select processstep_id from Process_Step where PS_Type_Name like 'Finished Goods') and product.processStep_id=Process_Step.ProcessStep_ID  
					UPDATE @SF_RESULTS SET PRODUCT =  @MKTG_PART_NUMBER WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR

					
					FETCH NEXT FROM @SF_CURSOR
					INTO @SF_CUSTOMER, @SF_PRODUCT, @SF_MONTH, @SF_YEAR, @SF_QUANTITY, @SF_UNIT_PRICE, @SF_CONFIDENCE_LEVEL			
			END	
					
						INSERT @FN_RESULTS SELECT SALES.CUSTOMER,SALES.PRODUCT,SALES.MONTH,SALES.YEAR,SALES.QUANTITY,SALES.UNIT_PRICE,SALES.CONFIDENCE_LEVEL,
						MKTG.QUANTITY,MKTG.UNIT_PRICE,MKTG.CONFIDENCE_LEVEL,'' FROM @SF_RESULTS SALES LEFT OUTER JOIN @MKTG_RESULTS MKTG ON 
						SALES.MONTH = MKTG.MONTH AND SALES.YEAR = MKTG.YEAR AND SALES.PRODUCT = MKTG.PRODUCT AND SALES.CUSTOMER = MKTG.CUSTOMER
								
	     	CLOSE @SF_CURSOR
		DEALLOCATE @SF_CURSOR
		
		
	END		      
	-- Ends here - Gandhi
/*	--ADDED BY VARMA TO STORE MISC FOREST DETAILS IN TO @FN_RESULTS
	INSERT INTO @FN_RESULTS SELECT CUSTOMER, PRODUCT, SF_MONTH MONTH, SF_YEAR , SUM(SF_QTY) QTY, MAX(SF_UNIT_PRICE) UNIT_PRICE,AVG(SF_CONFIDENCE_LEVEL),NULL,NULL,NULL,'MISC'  FROM MISC_SALES_FORECAST_MASTER 
				       WHERE ( ( SF_MONTH >= @MONTH_FROM AND SF_YEAR = @YEAR AND SF_MONTH < @TO_MONTH ) OR
		      			     ( SF_MONTH < @MONTH_TO AND SF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO ) OR
		      			     ( SF_YEAR < @YEAR_TO AND SF_YEAR > @YEAR ) )
				       GROUP BY CUSTOMER,PRODUCT,SF_MONTH,SF_YEAR  */

		SET @MISC_CURSOR = CURSOR FOR SELECT CUSTOMER, PRODUCT, SF_MONTH[MONTH], SF_YEAR[YEAR] , SF_QTY [QTY], SF_UNIT_PRICE [UNIT_PRICE], SF_CONFIDENCE_LEVEL  FROM MISC_SALES_FORECAST_MASTER 
				       WHERE ( ( SF_MONTH >= @MONTH_FROM AND SF_YEAR = @YEAR AND SF_MONTH < @TO_MONTH ) OR
		      			     ( SF_MONTH < @MONTH_TO AND SF_YEAR = @YEAR_TO AND @YEAR != @YEAR_TO ) OR
		      			     ( SF_YEAR < @YEAR_TO AND SF_YEAR > @YEAR ) )

		
		OPEN @MISC_CURSOR 
			FETCH NEXT FROM @MISC_CURSOR
			INTO  @SF_CUSTOMER, @SF_PRODUCT, @SF_MONTH, @SF_YEAR, @SF_QUANTITY, @SF_UNIT_PRICE, @SF_CONFIDENCE_LEVEL				
			WHILE @@FETCH_STATUS = 0
			BEGIN
					SELECT @ROW_COUNT = COUNT(*) FROM @MISC_RESULTS
					WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR
					
									
					IF ( @SF_CONFIDENCE_LEVEL IS NULL )
					BEGIN
						SET @SF_CONFIDENCE_LEVEL = 100.00	
					END

						
						IF ( @ROW_COUNT = 0 )
					        BEGIN
							INSERT INTO @MISC_RESULTS VALUES( @SF_CUSTOMER, @SF_PRODUCT, @SF_MONTH, @SF_YEAR, @SF_QUANTITY, @SF_UNIT_PRICE, @SF_CONFIDENCE_LEVEL)
						END
						ELSE
						BEGIN
							SELECT @SF_OLD_QUANTITY = QUANTITY,@SF_OLD_UNIT_PRICE = UNIT_PRICE FROM @MISC_RESULTS
							WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR 
							
							IF(@SF_UNIT_PRICE > @SF_OLD_UNIT_PRICE)
							BEGIN
								SET @SF_PRICE = @SF_UNIT_PRICE
							END
							ELSE
							BEGIN
								SET @SF_PRICE = @SF_OLD_UNIT_PRICE
							END

							SELECT @AVG_SF_CONFIDENCE_LEVEL = ROUND( AVG(SF_CONFIDENCE_LEVEL), 0 ) FROM MISC_SALES_FORECAST_MASTER WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND SF_MONTH = @SF_MONTH AND SF_YEAR = @SF_YEAR				
	
							UPDATE @MISC_RESULTS SET QUANTITY = (@SF_QUANTITY + @SF_OLD_QUANTITY), UNIT_PRICE = @SF_PRICE, CONFIDENCE_LEVEL = @AVG_SF_CONFIDENCE_LEVEL
							WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR
						END
	
					SET @MKTG_PART_NUMBER = NULL	-- Added by Gandhi on Oct 19th 2004				
					SELECT @MKTG_PART_NUMBER = Product.P_Marketing_part_Number from product,Process_Step  where Product.Part_Number = @SF_PRODUCT and product.ProcessStep_ID in (select processstep_id from Process_Step where PS_Type_Name like 'Finished Goods') and product.processStep_id=Process_Step.ProcessStep_ID  
					IF( @MKTG_PART_NUMBER IS NULL)
					BEGIN
						UPDATE @MISC_RESULTS SET PRODUCT =  @SF_PRODUCT WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR
					END
					ELSE
					BEGIN
						UPDATE @MISC_RESULTS SET PRODUCT =  @MKTG_PART_NUMBER WHERE CUSTOMER = @SF_CUSTOMER AND PRODUCT = @SF_PRODUCT AND MONTH = @SF_MONTH AND YEAR = @SF_YEAR
					END
				
					--SET @MKTG_PART_NUMBER = NULL    -- Commented by gandhi on Oct 19th 2004
					
					FETCH NEXT FROM @MISC_CURSOR
					INTO @SF_CUSTOMER, @SF_PRODUCT, @SF_MONTH, @SF_YEAR, @SF_QUANTITY, @SF_UNIT_PRICE, @SF_CONFIDENCE_LEVEL			
			END	
					
					INSERT INTO @FN_RESULTS SELECT CUSTOMER, PRODUCT, MONTH, YEAR , QUANTITY, UNIT_PRICE, CONFIDENCE_LEVEL, NULL, NULL,NULL,'MISC'  FROM @MISC_RESULTS 
														
	     	CLOSE @MISC_CURSOR
		DEALLOCATE @MISC_CURSOR



    RETURN

END

